package com.wwj.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wwj.common.constant.MessageConstant;
import com.wwj.common.context.BaseContext;
import com.wwj.common.result.PageResult;
import com.wwj.mapper.OrderMapper;
import com.wwj.pojo.dto.AdminOrderDTO;
import com.wwj.pojo.dto.OrderDTO;
import com.wwj.pojo.dto.OrderPageQueryDTO;
import com.wwj.pojo.entity.Order;
import com.wwj.pojo.entity.SetmealCheck;
import com.wwj.pojo.vo.OrderVO;
import com.wwj.pojo.vo.SetmealVO;
import com.wwj.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderMapper orderMapper;
    //新增预约单
    @Override
    public void save(OrderDTO orderDTO) {

        log.info("BaseContext.getCurrentId:{}",BaseContext.getCurrentId());
        Order order=Order.builder()
                .username(orderDTO.getUsername())
                .realname(orderDTO.getRealname())
                .idNumber(orderDTO.getIdNumber())
                .phone(orderDTO.getPhone())
                .sex(orderDTO.getSex())
                .categoryId(orderDTO.getCategoryId())
                .setmealname(orderDTO.getName())
                .price(orderDTO.getPrice())
                .image(orderDTO.getImage())
                .userId(BaseContext.getCurrentId())
                .createTime(LocalDateTime.now())
                .updateTime(LocalDateTime.now())
                .status(Order.STATUS1)
                .reportStatus(Order.REPORT_STATUS1)
                .number(String.valueOf(System.currentTimeMillis()))
                .build();
        orderMapper.insertOrder(order);
    }

    //删除体检预约单
    @Override
    public void deleteOrder(Long id) {
        Order order=orderMapper.selectById(id);
        if(order.getStatus() == Order.STATUS1 ){
            throw new RuntimeException(MessageConstant.STATUS_1_RESPORTSTATUS);
        }
        if(order.getStatus() == Order.STATUS2 && order.getReportStatus() == Order.REPORT_STATUS1){
            throw new RuntimeException(MessageConstant.STATUS_2_RESPORTSTATUS_1);
        }
        orderMapper.deleteById(id);
    }

    //修改预约单
    @Override
    public void update(AdminOrderDTO adminOrderDTO) {
        Order order=new Order();
        BeanUtils.copyProperties(adminOrderDTO,order);
        order.setUpdateTime(LocalDateTime.now());
        orderMapper.update(order);
    }



    //分页查询-管理端
    @Override
    public PageResult pageQuery(OrderPageQueryDTO orderPageQueryDTO) {
        PageHelper.startPage(orderPageQueryDTO.getPage(),orderPageQueryDTO.getPageSize());
        Page<OrderVO> page=orderMapper.pageQuery(orderPageQueryDTO);
        long total = page.getTotal();
        List<OrderVO> result = page.getResult();
        return new PageResult(total,result);
    }

    //分页查询-用户端
    @Override
    public PageResult pageQueryUser(OrderPageQueryDTO orderPageQueryDTO) {
        PageHelper.startPage(orderPageQueryDTO.getPage(),orderPageQueryDTO.getPageSize());
        orderPageQueryDTO.setUserId(BaseContext.getCurrentId());
        Page<OrderVO> page=orderMapper.pageQueryUser(orderPageQueryDTO);
        long total = page.getTotal();
        List<OrderVO> result = page.getResult();
        return new PageResult(total,result);
    }


}
